package com.ddwl.goods.dao;


import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.goods.dto.ItemDetailDto;
import com.ddwl.goods.dto.SkuDto;
import com.ddwl.goods.mapper.ItemMapper;
import com.ddwl.goods.model.Item;
import com.ddwl.goods.model.ItemSgin;
import com.ddwl.schema.bo.goods.ItemListBo;
import com.ddwl.schema.bo.goods.ItemSearchBo;
import com.ddwl.schema.bo.goods.retail.*;
import com.ddwl.schema.vo.goods.ItemVo;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Repository
public class ItemDao extends TkDao<ItemMapper,Item> {

    @Resource
    private ItemMapper itemMapper;

    public List<Item> findConAll(ItemSearchBo params){
        return itemMapper.findConAll(params);
    }

    public  void updateCommission(Item item){
        this.updateByPrimaryKeySelective(item);
    }

    /**
     * 更新供应商
     * @param item
     */
    public void updateSupplierAndCategoryCode(Item item){
        Item entity = new Item();
        entity.setSupplierId(item.getSupplierId());
        entity.setCategoryCode(item.getCategoryCode());
        Example example = getExample();
        example.and().andEqualTo("categoryId", item.getCategoryId());
        itemMapper.updateByExampleSelective(item, example);
    }

    public void itemSort(String itemId,Integer itemSort){
        Item item = new Item();
        item.setId(itemId);
        item.setItemSort(itemSort);
        this.updateByPrimaryKeySelective(item);
    }

    public void updateItemSgins(ItemSgin itemSgin){
        Item item = new Item();
        item.setItemSgin(itemSgin.getItemSgin());
        Example example = getExample();
        example.and().andIn("id", itemSgin.getItemIds());
        itemMapper.updateByExampleSelective(item, example);
    }

    public void updateLimitTag(String itemId,Integer limitTimeFlag){
        Item item = new Item();
        item.setLimitTimeFlag(limitTimeFlag);
        Example example = getExample();
        example.and().andEqualTo("id",itemId);
        itemMapper.updateByExampleSelective(item, example);
    }

    public void batchupdateLimitTag(List<String> itemId,Integer limitTimeFlag){
        Item item = new Item();
        item.setLimitTimeFlag(limitTimeFlag);
        Example example = getExample();
        example.and().andIn("id",itemId);
        itemMapper.updateByExampleSelective(item, example);
    }

    public void updateItemSginNull(ItemSgin itemSgin){
        itemMapper.updateItemSginNull(itemSgin);
    }

    public List<SkuDto> querySkuWithItem(ItemListBo param){
        return itemMapper.querySkuWithItem(param);
    }

    public Integer querySkuWithItemCount(ItemListBo param){
        return itemMapper.querySkuWithItemCount(param);
    }

    public ItemVo getDetailsById(String id) {
        return itemMapper.getDetailsById(id);
    }

    public List<ItemVo> findListByIds(List<String> ids) {
        return itemMapper.findListByIds(ids);
    }

    public List<Item> findItemListByIds(List<String> ids) {
        return selectByPrimaryKeys(ids);
    }

    public List<Item> getList(ShopItemListBo param) {
        Example example = getExample();
        Example.Criteria criteria = example.and();
        criteria.andEqualTo("shopId", param.getShopId())
                .andEqualTo("itemStatus", BaseEnum.StatusEnum.ENABLE.getCode())
                .andLike("categoryCode", MySqlUtil.rightLike(param.getCategoryCode()));
        if (BaseEnum.StatusEnum.ENABLE.getCode().equals(param.getProfitItem())) {
            criteria.andGreaterThan("itemCommission", BigDecimal.ZERO);
        }
        return itemMapper.selectByExample(example);
    }

    public List<Item> geShopConsoleList(ShopItemPageBo param) {
        Example example = getExample();
        example.and().andEqualTo("shopId", param.getShopId())
                .andEqualTo("itemStatus", param.getItemStatus())
                .andEqualTo("id", param.getId())
                .andEqualTo("itemName", param.getItemName())
                .andEqualTo("categoryId", param.getCategoryId())
                .andEqualTo("barcode", param.getBarcode())
                .andGreaterThanOrEqualTo("gmtCreated", param.getStartTime())
                .andLessThanOrEqualTo("gmtCreated", param.getEndTime());
        return itemMapper.selectByExample(example);
    }

    public List<Item> findItemByItemStatus(Integer byteValue) {
        Item item = new Item();
        item.setItemStatus(byteValue);
        return this.select(item);
    }

    public void updateSales(String id, Integer sales, Integer sumSales) {
        Item item = new Item();
        item.setId(id);
        item.setSales(sales);
        item.setSumSales(sumSales);
        this.updateByPrimaryKeySelective(item);
    }

    public ItemDetailDto getItemDetailById(String id) {
        return itemMapper.getItemDetailById(id);
    }

    public List<Item> getListByCategoryId(String categoryId) {
        Item item = new Item();
        item.setCategoryId(categoryId);
        return this.select(item);
    }

    public void updateCategoryCode(String categoryId, String categoryCode) {
        Item item = new Item();
        item.setCategoryCode(categoryCode);
        Example example = getExample();
        example.and().andEqualTo("categoryId", categoryId);
        this.itemMapper.updateByExampleSelective(item, example);
    }

    public void offShelfItemByCategoryIdList(List<String> categoryIds) {
        Item item = new Item();
        item.setItemStatus(BaseEnum.StatusEnum.DISABLE.getCode());
        Example example = getExample();
        example.and().andIn("categoryId", categoryIds);
        this.itemMapper.updateByExampleSelective(item, example);
    }

    public void updateItem(Item item) {
        itemMapper.updateByPrimaryKey(item);
    }

    public List<Item> listForMerchant(Item item) {
        Example example = getExample();
        example.and().andEqualTo("shopId", item.getShopId())
                .andEqualTo("itemStatus", item.getItemStatus())
                .andEqualTo("recommend",item.getRecommend())
                .andLike("itemName", MySqlUtil.allLike(item.getItemName()))
                .andLike("barcode", MySqlUtil.allLike(item.getBarcode()));
        return itemMapper.selectByExample(example);
    }

    public void changeCategoryCode(Map<String, String> map) {
        itemMapper.changeCategoryCode(map);
    }

    public List<Item> listHost(Item entity) {
        Example example = getExample();
        example.and().andEqualTo("shopId", entity.getShopId())
                .andEqualTo("itemStatus", entity.getItemStatus())
                .andEqualTo("hotFlag", entity.getHotFlag());
        example.orderBy("itemSort").asc();
        return itemMapper.selectByExample(example);
    }

    public List<Item> discountItemSelectList(DiscountItemSelectPageBo param) {
        Example example = getExample();
        example.and().andEqualTo("shopId", param.getShopId())
                .andEqualTo("itemStatus", BaseEnum.StatusEnum.ENABLE.getCode())
                .andNotEqualTo("discountFlag", BaseEnum.StatusEnum.ENABLE.getCode())
                .andNotEqualTo("limitTimeFlag", BaseEnum.StatusEnum.ENABLE.getCode());
        return itemMapper.selectByExample(example);
    }
    public List<Item> findAgentItemList(AgentItemPageBo param) {
        Example example = getExample();
        example.and().andEqualTo("shopId", param.getAgentShopId())
                .andEqualTo("itemStatus", BaseEnum.StatusEnum.ENABLE.getCode())
                .andLike("itemName", MySqlUtil.rightLike(param.getItemName()))
                .andEqualTo("agencyFlag", BaseEnum.StatusEnum.DISABLE.getCode());
        return itemMapper.selectByExample(example);
    }

    public void batchUpdateStock(List<Item> itemList) {
        itemMapper.batchUpdateStock(itemList);
    }

    public List<Item> getAgencyItemList(String shopId, List<String> barcodes) {
        Example example = getExample();
        example.and().andEqualTo("shopId", shopId)
                .andEqualTo("agencyFlag", BaseEnum.StatusEnum.ENABLE.getCode())
                .andIn("agencyItemCode",barcodes);
        return itemMapper.selectByExample(example);
    }

    public Item getActivityItem(String itemId) {
        Example example = getExample();
        example.and().andEqualTo("id", itemId);
        example.and().andEqualTo("discountFlag", BaseEnum.StatusEnum.ENABLE.getCode())
                .orEqualTo("limitTimeFlag", BaseEnum.StatusEnum.ENABLE.getCode());
        return itemMapper.selectOneByExample(example);
    }

    public void updateDiscountFlag(String discountItemId,Integer discountFlag) {
        Item item = new Item();
        item.setId(discountItemId);
        item.setDiscountFlag(discountFlag);
        this.updateByPrimaryKeySelective(item);
    }

    public void batchUpdateDiscountFlag(List<String> itemIds, Integer discountFlag) {
        if (!CollectionUtils.isEmpty(itemIds)) {
            Example example = getExample();
            example.and().andIn("id", itemIds);
            Item entity = new Item();
            entity.setDiscountFlag(discountFlag);
            itemMapper.updateByExampleSelective(entity, example);
        }
    }

    public void batchUpdateItemDiscountInfo(List<Item> items) {
        itemMapper.batchUpdateItemDiscountInfo(items);
    }

    public void batchShelf(ItemBatchShelfBo bo) {
        Example example = getExample();
        example.and().andIn("id", bo.getIds())
                .andEqualTo("shopId",bo.getShopId());
        Item entity = new Item();
        entity.setItemStatus(bo.getItemStatus());
        itemMapper.updateByExampleSelective(entity, example);
    }

    public List<Item> selectHasNoWxaCode() {
        Example example = getExample();
        example.and().andIsNull("wxaCode");
        return itemMapper.selectByExample(example);
    }
}
